import Vue from 'vue'
import Vuex from 'vuex'
import model from './models/model'

Vue.use(Vuex)

// const debug = process.env.NODE_ENV !== 'production'
import Hub from '@/api/hub'

export default new Vuex.Store({
    state: {
        checkoutStatus: null
    },
    modules: {
        model
    },
    strict: false,
    actions: {
        // Local emit fetchRemoteModelList
        fetchRemoteModelList() {
            Hub.fetchRemoteModelList()
        },

        // Remote emit modelList
        setModelList({commit}, modelList) {
            commit('setModelList', modelList)
            // commit(
            //     "initModelStateList",
            //     {
            //         'modelList': modelList,
            //         'initState': state.model.modelState.free.id
            //     }
            // )
        },

        // Local emit fetchRemoteModelDetailList
        fetchRemoteModelDetailList({state}) {
            Hub.fetchRemoteModelDetailList(state.model.selectedModelList)
        },

        // Remote emit modelDetailList
        setModelDetailList({commit}, modelDetailList) {
            commit('setModelDetailList', modelDetailList)
            commit('removeVirtualModels')
            commit('updateVirtualModels', {})
        },

        removeModelDetailList({commit}, modelList) {
            commit('removeModelDetailList', modelList)
            commit('removeVirtualModels')
            commit('updateVirtualModels', {})

        },

        updateVirtualModelMember({commit}, {vModelName, modelList}) {
            commit('removeVirtualModels', vModelName)
            commit('updateVirtualModels', {vModelName, modelList})
        },

        syncVirtualSettingToReal({commit}) {
            commit('syncVirtualSettingsToReal')
            // recalc virtual models
            commit('removeVirtualModels')
            commit('updateVirtualModels', {})
        },

        setModelStateList({commit}, modelStateList) {
            if (modelStateList.length === 0) {
                commit('resetModelStateList')
            } else {
                for (let modelState of modelStateList) {
                    commit('setModelState', modelState)
                }
            }
        },

        setModelState({commit}, modelState) {
            commit('setModelState', modelState)
        },

        remoteStartReleasing({state}, modelNameList) {
            let setting = {}
            for (let modelName of modelNameList) {
                setting[modelName] = state.model.modelSettingList[modelName]
            }
            if (Object.keys(setting).length > 0) {
                Hub.submitModelSettingList(setting)
            }
        },

        remoteStartUploading({state}) {
            Hub.uploadFiles(state.model.modelUploadFileList)
        },
    },
    mutations: {}
})
